home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2242 < prev    next >
Encoding:
Text File  |  1996-08-06  |  33.1 KB  |  839 lines

  1. Path: sun.soe.clarkson.edu!cline
  2. From: cline@sun.soe.clarkson.edu (Marshall Cline)
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ FAQ: posting #4/4
  5. Followup-To: comp.lang.c++
  6. Date: 16 Jan 1996 16:38:59 GMT
  7. Organization: Paradigm Shift, Inc (OO consulting/training)
  8. Sender: cline@sun.soe.clarkson.edu
  9. Distribution: world
  10. Expires: +1 month
  11. Message-ID: <4dgkb3$rlt@library.erc.clarkson.edu>
  12. Reply-To: cline@parashift.com (Marshall Cline)
  13. NNTP-Posting-Host: sun.soe.clarkson.edu
  14. Summary: Please read this before posting to comp.lang.c++
  15.  
  16. comp.lang.c++ Frequently Asked Questions list (with answers, fortunately).
  17. Copyright (C) 1991-95 Marshall P. Cline, Ph.D.
  18. Posting 4 of 4.
  19. Posting #1 explains copying permissions, (no)warranty, table-of-contents, etc
  20.  
  21. ==============================================================================
  22. SECTION 17: Linkage-to/relationship-with C
  23. ==============================================================================
  24.  
  25. Q105: How can I call a C function "f(int,char,float)" from C++ code?
  26.  
  27. Tell the C++ compiler that it is a C function:
  28.     extern "C" void f(int,char,float);
  29.  
  30. Be sure to include the full function prototype.  A block of many C functions
  31. can be grouped via braces, as in:
  32.  
  33.     extern "C" {
  34.       void* malloc(size_t);
  35.       char* strcpy(char* dest, const char* src);
  36.       int   printf(const char* fmt, ...);
  37.     }
  38.  
  39. ==============================================================================
  40.  
  41. Q106: How can I create a C++ function "f(int,char,float)" that is callable by
  42.    my C code?
  43.  
  44. The C++ compiler must know that "f(int,char,float)" is to be called by a C
  45. compiler using the same "extern C" construct detailed in the previous FAQ.
  46. Then you define the function in your C++ module:
  47.  
  48.     void f(int x, char y, float z)
  49.     {
  50.       //...
  51.     }
  52.  
  53. The "extern C" line tells the compiler that the external information sent to
  54. the linker should use C calling conventions and name mangling (e.g., preceded
  55. by a single underscore).  Since name overloading isn't supported by C, you
  56. can't make several overloaded fns simultaneously callable by a C program.
  57.  
  58. Caveats and implementation dependencies:
  59.  * your "main()" should be compiled with your C++ compiler (for static init).
  60.  * your C++ compiler should direct the linking process (for special libraries).
  61.  * your C and C++ compilers may need to come from same vendor and have
  62.    compatible versions (i.e., needs same calling convention, etc.).
  63.  
  64. ==============================================================================
  65.  
  66. Q107: Why's the linker giving errors for C/C++ fns being called from C++/C
  67.    fns?
  68.  
  69. See the previous two FAQs on how to use "extern "C"."
  70.  
  71. ==============================================================================
  72.  
  73. Q108: How can I pass an object of a C++ class to/from a C function?
  74.  
  75. Here's an example:
  76.  
  77.     /****** C/C++ header file: Fred.h ******/
  78.     #ifdef __cplusplus    /*"__cplusplus" is #defined if/only-if compiler is C++*/
  79.       extern "C" {
  80.     #endif
  81.  
  82.     #ifdef __STDC__
  83.       extern void c_fn(struct Fred*);    /* ANSI-C prototypes */
  84.       extern struct Fred* cplusplus_callback_fn(struct Fred*);
  85.     #else
  86.       extern void c_fn();            /* K&R style */
  87.       extern struct Fred* cplusplus_callback_fn();
  88.     #endif
  89.  
  90.     #ifdef __cplusplus
  91.       }
  92.     #endif
  93.  
  94.     #ifdef __cplusplus
  95.       class Fred {
  96.       public:
  97.         Fred();
  98.         void wilma(int);
  99.       private:
  100.         int a_;
  101.       };
  102.     #endif
  103.  
  104. "Fred.C" would be a C++ module:
  105.  
  106.     #include "Fred.h"
  107.     Fred::Fred() : a_(0) { }
  108.     void Fred::wilma(int a) : a_(a) { }
  109.  
  110.     Fred* cplusplus_callback_fn(Fred* fred)
  111.     {
  112.       fred->wilma(123);
  113.       return fred;
  114.     }
  115.  
  116. "main.C" would be a C++ module:
  117.  
  118.     #include "Fred.h"
  119.  
  120.     int main()
  121.     {
  122.       Fred fred;
  123.       c_fn(&fred);
  124.       return 0;
  125.     }
  126.  
  127. "c-fn.c" would be a C module:
  128.  
  129.     #include "Fred.h"
  130.     void c_fn(struct Fred* fred)
  131.     {
  132.       cplusplus_callback_fn(fred);
  133.     }
  134.  
  135. Passing ptrs to C++ objects to/from C fns will FAIL if you pass and get back
  136. something that isn't EXACTLY the same pointer.  For example, DON'T pass a base
  137. class ptr and receive back a derived class ptr, since your C compiler won't
  138. understand the pointer conversions necessary to handle multiple and/or virtual
  139. inheritance.
  140.  
  141. ==============================================================================
  142.  
  143. Q109: Can my C function access data in an object of a C++ class?
  144.  
  145. Sometimes.
  146.  
  147. (First read the previous FAQ on passing C++ objects to/from C functions.)
  148.  
  149. You can safely access a C++ object's data from a C function if the C++ class:
  150.  * has no virtual functions (including inherited virtual fns)
  151.  * has all its data in the same access-level section (private/protected/public)
  152.  * has no fully-contained subobjects with virtual fns
  153.  
  154. If the C++ class has any base classes at all (or if any fully contained
  155. subobjects have base classes), accessing the data will TECHNICALLY be
  156. non-portable, since class layout under inheritance isn't imposed by the
  157. language.  However in practice, all C++ compilers do it the same way: the base
  158. class object appears first (in left-to-right order in the event of multiple
  159. inheritance), and subobjects follow.
  160.  
  161. Furthermore, if the class (or any base class) contains any virtual functions,
  162. you can often (but less than always) assume a "void*" appears in the object
  163. either at the location of the first virtual function or as the first word in
  164. the object.  Again, this is not required by the language, but it is the way
  165. "everyone" does it.
  166.  
  167. If the class has any virtual base classes, it is even more complicated and less
  168. portable.  One common implementation technique is for objects to contain an
  169. object of the virtual base class (V) last (regardless of where "V" shows up as
  170. a virtual base class in the inheritance hierarchy).  The rest of the object's
  171. parts appear in the normal order.  Every derived class that has V as a virtual
  172. base class actually has a POINTER to the V part of the final object.
  173.  
  174. ==============================================================================
  175.  
  176. Q110: Why do I feel like I'm "further from the machine" in C++ as opposed to
  177.    C?
  178.  
  179. Because you are.
  180.  
  181. As an OOPL, C++ allows you to model the problem domain itself, which allows you
  182. to program in the language of the problem domain rather than in the language of
  183. the solution domain.
  184.  
  185. One of C's great strengths is the fact that it has "no hidden mechanism": what
  186. you see is what you get.  You can read a C program and "see" every clock cycle.
  187. This is not the case in C++; old line C programmers (such as many of us once
  188. were) are often ambivalent (can anyone say, "hostile") about this feature, but
  189. they soon realize that it provides a level of abstraction and economy of
  190. expression which lowers maintenance costs without destroying run-time
  191. performance.
  192.  
  193. Naturally you can write bad code in any language; C++ doesn't guarantee any
  194. particular level of quality, reusability, abstraction, or any other measure of
  195. "goodness."  C++ doesn't try to make it impossible for bad programmers to write
  196. bad programs; it enables reasonable developers to create superior software.
  197.  
  198. ==============================================================================
  199. SECTION 18: Pointers to member functions
  200. ==============================================================================
  201.  
  202. Q111: Is the type of "ptr-to-member-fn" different from "ptr-to-fn"?
  203.  
  204. Yep.
  205.  
  206. Consider the following function:
  207.  
  208.     int f(char a, float b);
  209.  
  210. If this is an ordinary function, its type is:    int (*)      (char,float);
  211. If this is a method of class Fred, its type is:  int (Fred::*)(char,float);
  212.  
  213. ==============================================================================
  214.  
  215. Q112: How do I pass a ptr to member fn to a signal handler, X event callback,
  216.    etc?
  217.  
  218. Don't.
  219.  
  220. Because a member function is meaningless without an object to invoke it on, you
  221. can't do this directly (if The X Windows System was rewritten in C++, it would
  222. probably pass references to OBJECTS around, not just pointers to fns; naturally
  223. the objects would embody the required function and probably a whole lot more).
  224.  
  225. As a patch for existing software, use a top-level (non-member) function as a
  226. wrapper which takes an object obtained through some other technique (held in a
  227. global, perhaps).  The top-level function would apply the desired member
  228. function against the global object.
  229.  
  230. E.g., suppose you want to call Fred::memfn() on interrupt:
  231.  
  232.     class Fred {
  233.     public:
  234.       void memfn();
  235.       static void staticmemfn();    //a static member fn can handle it
  236.       //...
  237.     };
  238.  
  239.     //wrapper fn remembers the object on which to invoke memfn in a global:
  240.     Fred* object_which_will_handle_signal;
  241.     void Fred_memfn_wrapper() { object_which_will_handle_signal->memfn(); }
  242.  
  243.     main()
  244.     {
  245.       /* signal(SIGINT, Fred::memfn); */   //Can NOT do this
  246.       signal(SIGINT, Fred_memfn_wrapper);  //Ok
  247.       signal(SIGINT, Fred::staticmemfn);   //Also Ok
  248.     }
  249.  
  250. Note: static member functions do not require an actual object to be invoked, so
  251. ptrs-to-static-member-fns are type compatible with regular ptrs-to-fns (see ARM
  252. ["Annotated Reference Manual"] p.25, 158).
  253.  
  254. ==============================================================================
  255.  
  256. Q113: Why do I keep getting compile errors (type mismatch) when I try to use a
  257.    member function as an interrupt service routine? 
  258.  
  259. This is a special case of the previous two questions, therefore read the
  260. previous two answers first.
  261.  
  262. Non-static member functions have a hidden parameter that corresponds to the
  263. 'this' pointer.  The 'this' pointer points to the instance data for the
  264. object.  The interrupt hardware/firmware in the system is not capable of
  265. providing the 'this' pointer argument.  You must use "normal" functions (non
  266. class members) or static member functions as interrupt service routines.
  267.  
  268. One possible solution is to use a static member as the interrupt service
  269. routine and have that function look somewhere to find the instance/member pair
  270. that should be called on interrupt.  Thus the effect is that a normal method
  271. is invoked on an interrupt, but for technical reasons you need to call an
  272. intermediate function first.
  273.  
  274. ==============================================================================
  275.  
  276. Q114: Why am I having trouble taking the address of a C++ function?
  277.  
  278. This is a corollary to the previous FAQ.
  279.  
  280. Long answer: In C++, member fns have an implicit parameter which points to the
  281. object (the "this" ptr inside the member fn).  Normal C fns can be thought of
  282. as having a different calling convention from member fns, so the types of their
  283. ptrs (ptr-to-member-fn vs ptr-to-fn) are different and incompatible.  C++
  284. introduces a new type of ptr, called a ptr-to-member, which can be invoked only
  285. by providing an object (see ARM ["Annotated Reference Manual"] 5.5).
  286.  
  287. NOTE: do NOT attempt to "cast" a ptr-to-mem-fn into a ptr-to-fn; the result is
  288. undefined and probably disastrous.  E.g., a ptr-to- member-fn is NOT required
  289. to contain the machine addr of the appropriate fn (see ARM, 8.1.2c, p.158).  As
  290. was said in the last example, if you have a ptr to a regular C fn, use either a
  291. top-level (non-member) fn, or a "static" (class) member fn.
  292.  
  293. ==============================================================================
  294.  
  295. Q115: How do I declare an array of pointers to member functions?
  296.  
  297. Keep your sanity with "typedef".
  298.  
  299.     class Fred {
  300.     public:
  301.       int f(char x, float y);
  302.       int g(char x, float y);
  303.       int h(char x, float y);
  304.       int i(char x, float y);
  305.       //...
  306.     };
  307.  
  308.     typedef  int (Fred::*FredPtr)(char x, float y);
  309.  
  310. Here's the array of pointers to member functions:
  311.  
  312.     FredPtr a[4] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };
  313.  
  314. To call one of the member functions on object "fred":
  315.  
  316.     void userCode(Fred& fred, int methodNum, char x, float y)
  317.     {
  318.       //assume "methodNum" is between 0 and 3 inclusive
  319.       (fred.*a[methodNum])(x, y);
  320.     }
  321.  
  322. You can make the call somewhat clearer using a #define:
  323.  
  324.     #define  callMethod(object,ptrToMethod)   ((object).*(ptrToMethod))
  325.     callMethod(fred, a[methodNum]) (x, y);
  326.  
  327. ==============================================================================
  328. SECTION 19: Container classes and templates
  329. ==============================================================================
  330.  
  331. Q116: How can I insert/access/change elements from a linked
  332.    list/hashtable/etc?
  333.  
  334. I'll use an "inserting into a linked list" as a prototypical example.  It's easy
  335. to allow insertion at the head and tail of the list, but limiting ourselves to
  336. these would produce a library that is too weak (a weak library is almost worse
  337. than no library).
  338.  
  339. This answer will be a lot to swallow for novice C++'ers, so I'll give a couple
  340. of options.  The first option is easiest; the second and third are better.
  341.  
  342. [1] Empower the "List" with a "current location," and methods such as
  343. advance(), backup(), atEnd(), atBegin(), getCurrElem(), setCurrElem(Elem),
  344. insertElem(Elem), and removeElem().  Although this works in small examples, the
  345. notion of "a" current position makes it difficult to access elements at two or
  346. more positions within the List (e.g., "for all pairs x,y do the following...").
  347.  
  348. [2] Remove the above methods from the List itself, and move them to a separate
  349. class, "ListPosition."  ListPosition would act as a "current position" within a
  350. List.  This allows multiple positions within the same List.  ListPosition would
  351. be a friend of List, so List can hide its innards from the outside world (else
  352. the innards of List would have to be publicized via public methods in List).
  353. Note: ListPosition can use operator overloading for things like advance() and
  354. backup(), since operator overloading is syntactic sugar for normal methods.
  355.  
  356. [3] Consider the entire iteration as an atomic event, and create a class
  357. template to embodies this event.  This enhances performance by allowing the
  358. public access methods (which may be virtual fns) to be avoided during the inner
  359. loop.  Unfortunately you get extra object code in the application, since
  360. templates gain speed by duplicating code.  For more, see [Koenig, "Templates as
  361. interfaces," JOOP, 4, 5 (Sept 91)], and [Stroustrup, "The C++ Programming
  362. Language Second Edition," under "Comparator"].
  363.  
  364. ==============================================================================
  365.  
  366. Q117: What's the idea behind "templates"?
  367.  
  368. A template is a cookie-cutter that specifies how to cut cookies that all look
  369. pretty much the same (although the cookies can be made of various kinds of
  370. dough, they'll all have the same basic shape).  In the same way, a class
  371. template is a cookie cutter to description of how to build a family of classes
  372. that all look basically the same, and a function template describes how to
  373. build a family of similar looking functions.
  374.  
  375. Class templates are often used to build type safe containers (although this
  376. only scratches the surface for how they can be used).
  377.  
  378. ==============================================================================
  379.  
  380. Q118: What's the syntax / semantics for a "function template"?
  381.  
  382. Consider this function that swaps its two integer arguments:
  383.  
  384.     void swap(int& x, int& y)
  385.     {
  386.       int tmp = x;
  387.       x = y;
  388.       y = tmp;
  389.     }
  390.  
  391. If we also had to swap floats, longs, Strings, Sets, and FileSystems, we'd get
  392. pretty tired of coding lines that look almost identical except for the type.
  393. Mindless repetition is an ideal job for a computer, hence a function template:
  394.  
  395.     template<class T>
  396.     void swap(T& x, T& y)
  397.     {
  398.       T tmp = x;
  399.       x = y;
  400.       y = tmp;
  401.     }
  402.  
  403. Every time we used "swap()" with a given pair of types, the compiler will go to
  404. the above definition and will create yet another "template function" as an
  405. instantiation of the above.  E.g.,
  406.  
  407.     main()
  408.     {
  409.       int    i,j;  /*...*/  swap(i,j);  //instantiates a swap for "int"
  410.       float  a,b;  /*...*/  swap(a,b);  //instantiates a swap for "float"
  411.       char   c,d;  /*...*/  swap(c,d);  //instantiates a swap for "char"
  412.       String s,t;  /*...*/  swap(s,t);  //instantiates a swap for "String"
  413.     }
  414.  
  415. (note: a "template function" is the instantiation of a "function template").
  416.  
  417. ==============================================================================
  418.  
  419. Q119: What's the syntax / semantics for a "class template"?
  420.  
  421. Consider a container class of that acts like an array of integers:
  422.  
  423.     //this would go into a header file such as "Array.h"
  424.     class Array {
  425.     public:
  426.       Array(int len=10)                  : len_(len), data_(new int[len]){}
  427.      ~Array()                            { delete [] data_; }
  428.       int len() const                    { return len_;     }
  429.       const int& operator[](int i) const { data_[check(i)]; }
  430.             int& operator[](int i)       { data_[check(i)]; }
  431.       Array(const Array&);
  432.       Array& operator= (const Array&);
  433.     private:
  434.       int  len_;
  435.       int* data_;
  436.       int  check(int i) const
  437.         { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
  438.           return i; }
  439.     };
  440.  
  441. Just as with "swap()" above, repeating the above over and over for Array of
  442. float, of char, of String, of Array-of-String, etc, will become tedious.
  443.  
  444.     //this would go into a header file such as "Array.h"
  445.     template<class T>
  446.     class Array {
  447.     public:
  448.       Array(int len=10)                : len_(len), data_(new T[len]) { }
  449.      ~Array()                          { delete [] data_; }
  450.       int len() const                  { return len_;     }
  451.       const T& operator[](int i) const { data_[check(i)]; }
  452.             T& operator[](int i)       { data_[check(i)]; }
  453.       Array(const Array<T>&);
  454.       Array& operator= (const Array<T>&);
  455.     private:
  456.       int len_;
  457.       T*  data_;
  458.       int check(int i) const
  459.         { if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
  460.           return i; }
  461.     };
  462.  
  463. Unlike template functions, template classes (instantiations of class templates)
  464. need to be explicit about the parameters over which they are instantiating:
  465.  
  466.     main()
  467.     {
  468.       Array<int>           ai;
  469.       Array<float>         af;
  470.       Array<char*>         ac;
  471.       Array<String>        as;
  472.       Array< Array<int> >  aai;
  473.     }              // ^^^-- note the space; do NOT use "Array<Array<int>>"
  474.                    //       (the compiler sees ">>" as a single token).
  475.  
  476. ==============================================================================
  477.  
  478. Q120: What is a "parameterized type"?
  479.  
  480. Another way to say, "class templates."
  481.  
  482. A parameterized type is a type that is parameterized over another type or some
  483. value.  List<int> is a type ("List") parameterized over another type ("int").
  484.  
  485. ==============================================================================
  486.  
  487. Q121: What is "genericity"?
  488.  
  489. Yet another way to say, "class templates."
  490.  
  491. Not to be confused with "generality" (which just means avoiding solutions which
  492. are overly specific), "genericity" means class templates.
  493.  
  494. ==============================================================================
  495. SECTION 20: Libraries
  496. ==============================================================================
  497.  
  498. Q122: Where can I get a copy of "STL"?
  499.  
  500. "STL" is the "Standard Templates Library".  You can get a copy from:
  501.  
  502. STL HP official site:    ftp://butler.hpl.hp.com/stl
  503. STL code alternate:    ftp://ftp.cs.rpi.edu/stl
  504. STL code + examples:    http://www.cs.rpi.edu/~musser/stl.html
  505.  
  506. STL hacks for GCC-2.6.3 are part of the GNU libg++ package 2.6.2.1 or later
  507. (and they may be in an earlier version as well).  Thanks to Mike Lindner.
  508.  
  509. ==============================================================================
  510.  
  511. Q123: Where can I ftp the code that accompanies "Numerical Recipes"?
  512.  
  513. This software is sold and there for it would be illegal to provide it on the
  514. net.  However, its only about $30.
  515.  
  516. ==============================================================================
  517.  
  518. Q124: Why is my executable so large?
  519.  
  520. Many people are surprised by how big executables are, especially if the source
  521. code is trivial.  For example, a simple "hello world" program can generate an
  522. executable that is larger than most people expect (40+K bytes).
  523.  
  524. One reason executables can be large is that portions of the C++ runtime
  525. library gets linked with your program. How much gets linked in depends on how
  526. much of it you are using, and on how the implementor split up the library into
  527. pieces.  For example, the iostream library is quite large, and consists of
  528. numerous classes and virtual functions. Using any part of it might pull in
  529. nearly all of the iostream code as a result of the interdependencies.
  530.  
  531. You might be able to make your program smaller by using a dynamically-linked
  532. version of the library instead of the static version.
  533.  
  534. You have to consult your compiler manuals or the vendor's technical support
  535. for a more detailed answer.
  536.  
  537. ==============================================================================
  538. SECTION 21: Nuances of particular implementations
  539. ==============================================================================
  540.  
  541. Q125: GNU C++ (g++) produces big executables for tiny programs; Why?
  542.  
  543. libg++ (the library used by g++) was probably compiled with debug info (-g).
  544. On some machines, recompiling libg++ without debugging can save lots of disk
  545. space (~1 Meg; the down-side: you'll be unable to trace into libg++ calls).
  546. Merely "strip"ping the executable doesn't reclaim as much as recompiling
  547. without -g followed by subsequent "strip"ping the resultant "a.out"s.
  548.  
  549. Use "size a.out" to see how big the program code and data segments really are,
  550. rather than "ls -s a.out" which includes the symbol table.
  551.  
  552. ==============================================================================
  553.  
  554. Q126: Is there a yacc-able C++ grammar?
  555.  
  556. Jim Roskind is the author of a yacc grammar for C++. It's roughly compatible
  557. with the portion of the language implemented by USL cfront 2.0 (no templates,
  558. no exceptions, no run-time-type-identification).  Jim's grammar deviates from
  559. C++ in a couple of minor-but-subtle ways.
  560.  
  561. The grammar can be accessed by anonymous ftp from the following sites:
  562.  * ics.uci.edu (128.195.1.1) in "gnu/c++grammar2.0.tar.Z".
  563.  * mach1.npac.syr.edu (128.230.7.14) in "pub/C++/c++grammar2.0.tar.Z".
  564.  
  565. ==============================================================================
  566.  
  567. Q127: What is C++ 1.2?  2.0?  2.1?  3.0?
  568.  
  569. These are not versions of the language, but rather versions of cfront, which
  570. was the original C++ translator implemented by AT&T.  It has become generally
  571. accepted to use these version numbers as if they were versions of the language
  572. itself.
  573.  
  574. *VERY* roughly speaking, these are the major features:
  575.  * 2.0 includes multiple/virtual inheritance and pure virtual functions.
  576.  * 2.1 includes semi-nested classes and "delete [] ptr_to_array."
  577.  * 3.0 includes fully-nested classes, templates and "i++" vs "++i."
  578.  * 4.0 will include exceptions.
  579.  
  580. ==============================================================================
  581.  
  582. Q128: If name mangling was standardized, could I link code compiled with
  583.    compilers from different compiler vendors?
  584.  
  585. Short answer: Probably not.
  586.  
  587. In other words, some people would like to see name mangling standards
  588. incorporated into the proposed C++ ANSI standards in an attempt to avoiding
  589. having to purchase different versions of class libraries for different
  590. compiler vendors.  However name mangling differences are one of the smallest
  591. differences between implementations, even on the same platform.  Here is a
  592. partial list of other differences:
  593.  
  594. 1) Number and type of hidden arguments to member functions.
  595.    1a) is 'this' handled specially?
  596.    1b) where is the return-by-value pointer passed?
  597. 2) Assuming a vtable is used:
  598.    2a) what is its contents and layout?
  599.    2b) where/how is the adjustment to 'this' made for multiple inheritance?
  600. 3) How are classes laid out, including:
  601.    3a) location of base classes?
  602.    3b) handling of virtual base classes?
  603.    3c) location of vtable pointers, if vtables are used?
  604. 4) Calling convention for functions, including:
  605.    4a) does caller or callee adjust the stack?
  606.    4b) where are the actual parameters placed?
  607.    4c) in what order are the actual parameters passed?
  608.    4d) how are registers saved?
  609.    4e) where does the return value go?
  610.    4f) special rules for passing or returning structs or doubles?
  611.    4g) special rules for saving registers when calling leaf functions?
  612. 5) How is the run-time-type-identification laid out?
  613. 6) How does the runtime exception handling system know which local objects
  614.    need to be destructed during an exception throw?
  615.  
  616. ==============================================================================
  617. SECTION 22: Miscellaneous technical and environmental issues
  618. ==============================================================================
  619. SUBSECTION 22A: Miscellaneous technical issues:
  620. ==============================================================================
  621.  
  622. Q129: Why are classes with static data members getting linker errors?
  623.  
  624. Static data members must be explicitly defined in exactly one module.  E.g.,
  625.  
  626.     class Fred {
  627.     public:
  628.       //...
  629.     private:
  630.       static int i_;  //declares static data member "Fred::i_"
  631.       //...
  632.     };
  633.  
  634. The linker will holler at you ("Fred::i_ is not defined") unless you define (as
  635. opposed to declare) "Fred::i_" in (exactly) one of your source files:
  636.  
  637.     int Fred::i_ = some_expression_evaluating_to_an_int;
  638. or:
  639.     int Fred::i_;
  640.  
  641. The usual place to define static data members of class "Fred" is file "Fred.C"
  642. (or "Fred.cpp", etc; whatever filename extension you use).
  643.  
  644. ==============================================================================
  645.  
  646. Q130: What's the difference between the keywords struct and class?
  647.  
  648. The members and base classes of a struct are public by default, while in class,
  649. they default to private.  Note: you should make your base classes EXPLICITLY
  650. public, private, or protected, rather than relying on the defaults.
  651.  
  652. "struct" and "class" are otherwise functionally equivalent.
  653.  
  654. ==============================================================================
  655.  
  656. Q131: Why can't I overload a function by its return type?
  657.  
  658. If you declare both "char f()" and "float f()", the compiler gives you an error
  659. message, since calling simply "f()" would be ambiguous.
  660.  
  661. ==============================================================================
  662.  
  663. Q132: What is "persistence"?  What is a "persistent object"?
  664.  
  665. A persistent object can live after the program which created it has stopped.
  666. Persistent objects can even outlive different versions of the creating program,
  667. can outlive the disk system, the operating system, or even the hardware on
  668. which the OS was running when they were created.
  669.  
  670. The challenge with persistent objects is to effectively store their method code
  671. out on secondary storage along with their data bits (and the data bits and
  672. method code of all member objects, and of all their member objects and base
  673. classes, etc).  This is non-trivial when you have to do it yourself.  In C++,
  674. you have to do it yourself.  C++/OO databases can help hide the mechanism for
  675. all this.
  676.  
  677. ==============================================================================
  678.  
  679. Q133: Why is floating point so inaccurate?  Why doesn't this print 0.43?
  680.  
  681.     #include<iostream.h> 
  682.  
  683.     main()
  684.     {
  685.       float a = 1000.43;
  686.       float a = 1000.0;
  687.       cout << a - b << '\n';
  688.     } 
  689.  
  690. (note, on one C++ implementation, this prints 0.429993) 
  691.  
  692. Disclaimer: Frustration with rounding/truncation/approximation isn't really a
  693. C++ issue.  It's a computer science issue.  However, people keep asking about
  694. it on comp.lang.c++, so here's a nominal answer.
  695.  
  696. Answer: Floating point is an approximation.  The IEEE standard for 32 bit
  697. float supports 1 bit of sign, 8 bits of exponent, and 23 bits of mantissa.
  698. Since a normalized binary-point mantissa always has the form 1.xxxxx... the
  699. leading 1 is dropped and you get effectively 24 bits of mantissa.  The number
  700. 1000.43 (and many, many others) is not exactly representable in float or
  701. double format.  1000.43 is actually represented as the following bitpattern
  702. (the 's' shows the position of the sign bit, the 'e's show the positions of
  703. the exponent bits, and the 'm's show the positions of the mantissa bits):
  704.  
  705.     seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm 
  706.     01000100011110100001101110000101 
  707.  
  708. The shifted mantissa is 1111101000.01101110000101 or 1000 + 7045/16384.  The
  709. fractional part is 0.429992675781.  With 24 bits of mantissa you only get
  710. about 1 part in 16M of precision for float.  The 'double' type provides more
  711. precision (53 bits of mantissa).
  712.  
  713. ==============================================================================
  714. SUBSECTION 22B: Miscellaneous environmental issues:
  715. ==============================================================================
  716.  
  717. Q134: Is there a TeX or LaTeX macro that fixes the spacing on "C++"?
  718.  
  719. Yes, here are two:
  720.  
  721. \def\CC{C\raise.22ex\hbox{{\footnotesize +}}\raise.22ex\hbox{\footnotesize +}}
  722.  
  723. \def\CC{{C\hspace{-.05em}\raisebox{.4ex}{\tiny\bf ++}}}
  724.  
  725. ==============================================================================
  726.  
  727. Q135: Where can I access C++2LaTeX, a LaTeX pretty printer for C++ source?
  728.  
  729. Here are a few ftp locations:
  730.  
  731. Host aix370.rrz.uni-koeln.de   (134.95.80.1) Last updated 15:41 26 Apr 1991
  732.     Location: /tex
  733.       FILE      rw-rw-r--     59855  May  5  1990   C++2LaTeX-1.1.tar.Z
  734. Host utsun.s.u-tokyo.ac.jp   (133.11.11.11) Last updated 05:06 20 Apr 1991
  735.     Location: /TeX/macros
  736.       FILE      rw-r--r--     59855  Mar  4 08:16   C++2LaTeX-1.1.tar.Z
  737. Host nuri.inria.fr   (128.93.1.26) Last updated 05:23  9 Apr 1991
  738.     Location: /TeX/tools
  739.       FILE      rw-rw-r--     59855  Oct 23 16:05   C++2LaTeX-1.1.tar.Z
  740. Host iamsun.unibe.ch   (130.92.64.10) Last updated 05:06  4 Apr 1991
  741.     Location: /TeX
  742.       FILE      rw-r--r--     59855  Apr 25  1990   C++2LaTeX-1.1.tar.Z
  743. Host iamsun.unibe.ch   (130.92.64.10) Last updated 05:06  4 Apr 1991
  744.     Location: /TeX
  745.       FILE      rw-r--r--     51737  Apr 30  1990
  746.       C++2LaTeX-1.1-PL1.tar.Z
  747. Host tupac-amaru.informatik.rwth-aachen.de   (192.35.229.9) Last updated 05:07 18 Apr 1991
  748.     Location: /pub/textproc/TeX
  749.       FILE      rw-r--r--     72957  Oct 25 13:51  C++2LaTeX-1.1-PL4.tar.Z
  750. Host wuarchive.wustl.edu   (128.252.135.4) Last updated 23:25 30 Apr 1991
  751.     Location: /packages/tex/tex/192.35.229.9/textproc/TeX
  752.       FILE      rw-rw-r--     49104  Apr 10  1990   C++2LaTeX-PL2.tar.Z
  753.       FILE      rw-rw-r--     25835  Apr 10  1990   C++2LaTeX.tar.Z
  754. Host tupac-amaru.informatik.rwth-aachen.de   (192.35.229.9) Last updated 05:07 18 Apr 1991
  755.     Location: /pub/textproc/TeX
  756.       FILE rw-r--r-- 74015  Mar 22 16:23 C++2LaTeX-1.1-PL5.tar.Z
  757.     Location: /pub
  758.       FILE rw-r--r-- 74015  Mar 22 16:23 C++2LaTeX-1.1-PL5.tar.Z
  759. Host sol.cs.ruu.nl   (131.211.80.5) Last updated 05:10 15 Apr 1991
  760.     Location: /TEX/TOOLS
  761.       FILE      rw-r--r--     74015  Apr  4 21:02x   C++2LaTeX-1.1-PL5.tar.Z
  762. Host tupac-amaru.informatik.rwth-aachen.de (192.35.229.9) Last updated 05:07 18 Apr 1991
  763.     Location: /pub/textproc/TeX
  764.       FILE      rw-r--r--      4792  Sep 11  1990 C++2LaTeX-1.1-patch#1
  765.       FILE      rw-r--r--      2385  Sep 11  1990 C++2LaTeX-1.1-patch#2
  766.       FILE      rw-r--r--      5069  Sep 11  1990 C++2LaTeX-1.1-patch#3
  767.       FILE      rw-r--r--      1587  Oct 25 13:58 C++2LaTeX-1.1-patch#4
  768.       FILE      rw-r--r--      8869  Mar 22 16:23 C++2LaTeX-1.1-patch#5
  769.       FILE      rw-r--r--      1869  Mar 22 16:23 C++2LaTeX.README
  770. Host rusmv1.rus.uni-stuttgart.de   (129.69.1.12) Last updated 05:13 13 Apr 1991
  771.     Location: /soft/tex/utilities
  772.       FILE      rw-rw-r--    163840  Jul 16  1990   C++2LaTeX-1.1.tar
  773.  
  774. ==============================================================================
  775.  
  776. Q136: Where can I access "tgrind," a pretty printer for C++/C/etc source?
  777.  
  778. "tgrind" reads a C++ source file, and spits out something that looks pretty on
  779. most Unix printers.  It usually comes with the public distribution of TeX and
  780. LaTeX; look in the directory: "...tex82/contrib/van/tgrind".  A more up-to-date
  781. version of tgrind by Jerry Leichter can be found on: venus.ycc.yale.edu in
  782. [.TGRIND].
  783.  
  784. ==============================================================================
  785.  
  786. Q137: Is there a C++-mode for GNU emacs?  If so, where can I get it?
  787.  
  788. Yes, there is a C++-mode for GNU emacs.
  789.  
  790. The latest and greatest version of C++-mode (and c-mode) is implemented in the
  791. file cc-mode.el.  It is an extension of Detlef & Clamen's version.
  792. A version is included with emacs.  Newer version are availiable from
  793. the elisp archives.
  794.  
  795. ==============================================================================
  796.  
  797. Q138: Where can I get OS-specific FAQs answered (e.g.,BC++,DOS,Windows,etc)?
  798.  
  799. See one of the following:
  800.  * comp.os.msdos.programmer
  801.  * comp.windows.ms.programmer
  802.  * comp.unix.programmer
  803.  
  804. [If anyone has an email address for a BC++, VC++, or Semantic C++ bug list
  805. and/or discussion mailing list, please let me know how to subscribe, and I'll
  806. mention it here].
  807.  
  808. ==============================================================================
  809.  
  810. Q139: Why does my DOS C++ program says "Sorry: floating point code not
  811.    linked"?
  812.  
  813. The compiler attempts to save space in the executable by not including the
  814. float-to-string format conversion routines unless they are necessary, but
  815. sometimes it guesses wrong, and gives you the above error message.  You can fix
  816. this by (1) using <iostream.h> instead of <stdio.h>, or (2) by including the
  817. following function somewhere in your compilation (but don't call it!):
  818.  
  819.     static void dummyfloat(float *x) { float y; dummyfloat(&y); }
  820.  
  821. See FAQ on stream I/O for more reasons to use <iostream.h> vs <stdio.h>.
  822.  
  823. ==============================================================================
  824.  
  825. Q140: Why does my BC++ Windows app crash when I'm not running the BC45 IDE?
  826.  
  827. If you're using BC++ for a Windows app, and it works ok as long as you have the
  828. BC45 IDE running, but when the BC45 IDE is shut down you get an exception
  829. during the creation of a window, then add the following line of code to the
  830. InitMainWindow() method of your application ("YourApp::InitMainWindow()"):
  831.  
  832.     EnableBWCC(TRUE);
  833.  
  834. ==============================================================================
  835.  
  836. --
  837. Paradigm Shift, Inc. / P.O. Box 5108 / Potsdam, NY  13676
  838. Voice: 315-353-6100 / FAX: 315-353-6110
  839.